package com.poly.cms.controller;

import com.poly.cms.pojo.dto.CmsContentDTO;
import com.poly.cms.pojo.query.CmsContentQuery;
import com.poly.cms.pojo.vo.CmsContentVO;
import com.poly.cms.service.ICmsContentService;
import com.poly.common.core.domain.R;
import com.poly.common.core.validate.Add;
import com.poly.common.core.validate.Update;
import com.poly.common.core.web.controller.BaseController;
import com.poly.common.core.web.page.Pagination;
import com.poly.common.core.web.page.TableData;
import com.poly.common.log.annotation.Log;
import com.poly.common.log.enums.BusinessType;
import com.poly.common.security.annotation.RequiresPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 内容Controller
 *
 * @author poly
 * @date 2023-10-25
 */
@Api(tags = "内容")
@RestController
@RequestMapping("/content")
public class ContentController extends BaseController {
    @Autowired
    private ICmsContentService cmsContentService;

    /**
     * 查询内容列表
     */
    @ApiOperation("查询内容分页")
    @RequiresPermissions("cms:content:list")
    @GetMapping("/queryPage")
    public R<Pagination<CmsContentVO>> queryPage(CmsContentQuery query) {
        return R.ok(cmsContentService.queryPage(query));
    }

    /**
     * 查询内容列表
     */
    @ApiOperation("查询内容列表")
    @RequiresPermissions("cms:content:list")
    @GetMapping("/list")
    public TableData<CmsContentVO> list(CmsContentQuery query) {
        startPage();
        return TableData.data(cmsContentService.queryList(query));
    }

    /**
     * 获取内容详细信息
     */
    @ApiOperation("获取内容详细信息")
    @RequiresPermissions("cms:content:query")
    @GetMapping(value = "/{id}")
    public R<CmsContentVO> getInfo(@PathVariable("id") Long id) {
        return R.ok(cmsContentService.getVO(id));
    }

    /**
     * 新增内容
     */
    @ApiOperation("新增内容")
    @Log(title = "内容管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("cms:content:add")
    @PostMapping
    public R add(@RequestBody @Validated({Add.class}) CmsContentDTO dto) {
        return toResult(cmsContentService.add(dto));
    }

    /**
     * 修改内容
     */
    @ApiOperation("修改内容")
    @Log(title = "内容管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("cms:content:edit")
    @PutMapping
    public R edit(@RequestBody @Validated({Update.class}) CmsContentDTO dto) {
        return toResult(cmsContentService.update(dto));
    }

    /**
     * 删除内容
     */
    @ApiOperation("删除内容")
    @Log(title = "内容管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("cms:content:remove")
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable Long[] ids) {
        return toResult(cmsContentService.deleteByIds(ids));
    }
}
